package com.java.controller;


import com.alibaba.fastjson.JSONArray;
import com.java.pojo.EnterGood;
import com.java.pojo.Lack;
import com.java.service.EnterGoodService;
import com.java.service.EnterGoodsService;
import com.java.service.LackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/enterGoods")
public class EnterGoods {

    @Autowired
    private EnterGoodsService enterGoodsService;

    @Autowired
    private EnterGoodService enterGoodService;




    @Autowired
    private LackService lackService;

    /*查询*/
    @RequestMapping("/findAll")
    public String findAll(@RequestParam("eid") Integer eid,
                          @RequestParam("document") String document,
                          @RequestParam("rid") String rid,
                          @RequestParam("goodstyle") String goodstyle,
                          Model model,
                          HttpSession session){
        List<com.java.pojo.EnterGoods> list = enterGoodsService.getListByGoodS(eid, document);
        System.out.println(document+"===========");
        model.addAttribute("list",list);
        model.addAttribute("eid",eid);
        model.addAttribute("rid",rid);
        model.addAttribute("goodstyle",goodstyle);
        model.addAttribute("document",document);
        return "enterGoods/showEnterGoods";
    }

    /*删除*/
    @RequestMapping(value = "/del",produces = "text/html;charset=UTF-8",method = RequestMethod.GET)
    @ResponseBody
    public String  del(@RequestParam(value = "eid",required = false) String eid,Model model){
        System.out.println("eid:"+eid);
        boolean b = enterGoodsService.deleteEnterGoodByEgid(eid);
        if(b){
            return "<script>alert('删除成功');location.href='/EnterGood/EnterPageGood?document=';</script>";
        }else {
            return "<script>alert('删除失败');location.href='/EnterGood/EnterPageGood?document=';</script>";
        }
    }



    /*进入添加页面*/
    @RequestMapping("/preAdd")
    public String priAdd(Model model,
                         @RequestParam(value = "rid",required = false) Integer rid,
                         @RequestParam(value = "egid",required = false) Integer egid,
                         @RequestParam(value = "goodstyle",required = false) Integer goodstyle
                         ){
        System.out.println("rid:"+rid+"egid"+egid+"goodstyle"+goodstyle);
        List<Lack> lackListByOKAndGS = lackService.getLackListByOKAndGS(goodstyle);
        model.addAttribute("lack",lackListByOKAndGS);
        model.addAttribute("rid",rid);
        model.addAttribute("egid",egid);
        model.addAttribute("goodstyle",goodstyle);
        return "enterGoods/addEnterGoods";
    }


    /*添加*/
    @RequestMapping(value = "/addEnterGoods",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String addEnterGoods(com.java.pojo.EnterGoods enterGoods){
        boolean b = enterGoodsService.addEnterGoods(enterGoods);
        int i = lackService.updOKandNo4(enterGoods.getGoodname2());
        System.out.println("---i"+i);
        if(b){
            return "<script>alert('successful');location.href='/EnterGood/EnterPageGood?document=';</script>";
        }else {
            return "<script>alert('订单号重复,请重新添加');location.href='/EnterGood/EnterPageGood?document=';</script>";
        }
    }

    //根据id查询entergoods外键的产品具体内容
    @RequestMapping("/findEnterGoodsByEod")
    public String findEnterGoodsByEod(@RequestParam("eid") Integer eid,Model model){
        com.java.pojo.EnterGoods goods = enterGoodsService.getEnterGoodsByEid(eid);
        if(goods!=null){
            System.out.println("goods:"+goods);
            model.addAttribute("goods",goods);
            return "enterGoods/showEnterGoodsByEid";
        }else {
            return "error";
        }
        }


        //进入修改页面
        @RequestMapping("/preUpdateEnterGoods")
    public String preUpdateEnterGoods(@RequestParam("eid") Integer eid,Model model) {
            com.java.pojo.EnterGoods goods = enterGoodsService.getEnterGoodsByEid(eid);
            if (goods != null) {
                System.out.println("goods:" + goods);
                model.addAttribute("goods", goods);
                return "enterGoods/updateEnterGoods2";
            } else {
                return "error";
            }
        }

        //修改
        @RequestMapping(value = "/upd",produces = "text/html;charset=UTF-8")
        @ResponseBody
        public String upd(  com.java.pojo.EnterGoods enterGoods
                      ){
            System.out.println(enterGoods);
            int i = enterGoodsService.updateByEnterGoodAndEnterGoods(enterGoods.getEid(), enterGoods.getNum(), enterGoods.getMoney());
            if(i==2){
                return "<script>alert('修改成功');location.href='/EnterGood/EnterPageGood?document=';</script>";
            }else {
                return "<script>alert('修改失败!!');location.href='/EnterGood/EnterPageGood?document=';</script>";
            }
        }

    @RequestMapping("/ajaxLack")
    @ResponseBody
    public String lackFindAJAX(@RequestParam("goods2") String goods2,@RequestParam("num") String num){
        HashMap<String,String> map=new HashMap<>();
        Lack lackByName = lackService.getLackByName(goods2);
        System.out.println("en"+lackByName);
        if(Integer.parseInt(num)<=lackByName.getNum3()){
            map.put("userCode","exist");
        }else{
            map.put("userCode","no");
        }
        //将map转为json
        return JSONArray.toJSONString(map);
    }
}
